home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / thesource6.dms / thesource6.adf / Source / Misc / ReactDiff.lha / ReactionDiffusion / linear.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-29  |  5.0 KB  |  238 lines

  1. /*
  2.  
  3. This creates a one-dimensional graph of peaks and valleys that represent
  4. the result of a reaction-diffusion system.
  5.  
  6. The particular system is one originally described by Alan Turing, and
  7. a clear description of it can be found in:
  8.  
  9.   How Well Does Turing's Theory of Morphogenesis Work?
  10.   Jonathan Bard and Ian Lauder
  11.   Journal of Theoretical Biology, Vol. 45, No. 2, pp. 501-531.
  12.   (June 1974)
  13.  
  14.  
  15. Permission is granted to modify and/or distribute this program so long
  16. as the program is distributed free of charge and this header is retained
  17. as part of the program.
  18.  
  19. Copyright (c) Greg Turk, 1991
  20.  
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <math.h>
  25.  
  26. extern double atof();
  27. extern double drand48();
  28. float frand();
  29.  
  30. /* screen stuff */
  31.  
  32. int xsize = 1000;
  33. int ysize = 300;
  34.  
  35. #define  BLACK    0
  36. #define  RED     10
  37. #define  GREEN   20
  38. #define  BLUE    30
  39. #define  YELLOW  40
  40. #define  CYAN    50
  41. #define  MAGENTA 60
  42. #define  WHITE  255
  43.  
  44. /* simulation variables */
  45.  
  46. #define MAX 1400
  47.  
  48. float a[MAX];            /* concentration of chemical "a" */
  49. float b[MAX];            /* concentration of chemical "b" */
  50.  
  51. float da[MAX];            /* change in "a" */
  52. float db[MAX];            /* change in "b" */
  53.  
  54. float ainit = 4;        /* initial concentration of "a" */
  55. float binit = 4;        /* initial concentration of "b" */
  56.  
  57. float diff1 = 0.25;        /* diffusion rate for "a" */
  58. float diff2 = 0.0625;        /* diffusion rate for "b" */
  59.  
  60. float beta[MAX];        /* random substrate */
  61. float beta_init = 12;        /* initial value of substrate */
  62. float beta_rand = 0.05;        /* random variation in substrate */
  63.  
  64. float react_speed = 1.0;    /* speed of reaction (versus diffusion) */
  65.  
  66. int ncells = 60;        /* number of cells in the line */
  67.  
  68. int interval = 100;        /* frequency of display */
  69.  
  70.  
  71. /******************************************************************************
  72. Main routine.
  73. ******************************************************************************/
  74.  
  75. main (argc,argv)
  76.   int  argc;
  77.   char *argv[];
  78. {
  79.   char *s;
  80.  
  81.   /* parse the command line arguments */
  82.  
  83.   while(--argc >0 && (*++argv)[0]=='-') {
  84.     for(s = argv[0]+1; *s; s++)
  85.       switch(*s) {
  86.     case 's':
  87.       xsize = atoi (*++argv);
  88.       ysize = atoi (*++argv);
  89.       argc -= 2;
  90.       break;
  91.     case 'n':
  92.       ncells = atoi (*++argv);
  93.       argc -= 1;
  94.       break;
  95.     case 'r':
  96.       react_speed = atof (*++argv);
  97.       argc -= 1;
  98.       break;
  99.     default:
  100.       break;
  101.       }
  102.   }
  103.  
  104.   /* set up graphics */
  105.  
  106.   window_corner (512 - xsize / 2, 20);
  107.   init_graphics (xsize, ysize, 40);
  108.  
  109.   makecolor (BLACK, 0, 0, 0);
  110.   makecolor (RED, 255, 0, 0);
  111.   makecolor (GREEN, 0, 255, 0);
  112.   makecolor (BLUE, 0, 0, 255);
  113.   makecolor (CYAN, 0, 255, 255);
  114.   makecolor (MAGENTA, 255, 0, 255);
  115.   makecolor (YELLOW, 255, 255, 0);
  116.   makecolor (WHITE, 255, 255, 255);
  117.  
  118.   turing (999999);
  119.  
  120.   /* busy wait */
  121.  
  122.   while (1);
  123. }
  124.  
  125.  
  126. /******************************************************************************
  127. Compute Turing reaction-diffusion.
  128. ******************************************************************************/
  129.  
  130. turing(steps)
  131.   int steps;
  132. {
  133.   int i,i0,i1,k;
  134.   float adiff,bdiff;
  135.   float rsp;
  136.  
  137.   rsp = react_speed / 16;
  138.  
  139.   /* initialize values of all cells */
  140.  
  141.   for (i = 0; i < ncells; i++) {
  142.     a[i] = ainit;
  143.     b[i] = binit;
  144.     beta[i] = beta_init + frand (-beta_rand, beta_rand);
  145.   }
  146.  
  147.   /* compute simulation steps */
  148.  
  149.   for (k = 1; k <= steps; k++) {
  150.  
  151.     /* compute one step */
  152.  
  153.     for (i = 0; i < ncells; i++) {
  154.  
  155.       /* wrap around the end of the line of cells */
  156.       i0 = (i+ncells-1) % ncells;
  157.       i1 = (i+1) % ncells;
  158.  
  159.       /* diffusion values for "a" and "b" */
  160.       adiff = a[i0] + a[i1] - 2 * a[i];
  161.       bdiff = b[i0] + b[i1] - 2 * b[i];
  162.  
  163.       /* save the reaction and diffusion values values */
  164.       da[i] = rsp * (16 - a[i] * b[i]) + adiff * diff1;
  165.       db[i] = rsp * (a[i] * b[i] - b[i] - beta[i]) + bdiff * diff2;
  166.     }
  167.  
  168.     /* add the change in concentration */
  169.  
  170.     for (i = 0; i < ncells; i++) {
  171.       a[i] += da[i];
  172.       b[i] += db[i];
  173.       if (b[i] < 0)
  174.     b[i] = 0;
  175.     }
  176.  
  177.     /* graph the values if it is time */
  178.  
  179.     if (k % interval == 0 || k == steps)
  180.       draw_turing();
  181.   }
  182. }
  183.  
  184.  
  185. /******************************************************************************
  186. Draw the current chemical concentration in a Turing reaction-diffusion
  187. system.
  188. ******************************************************************************/
  189.  
  190. draw_turing()
  191. {
  192.   int i;
  193.   int x,y;
  194.   int x_old,y_old;
  195.   float min,max;
  196.   float s,t;
  197.  
  198.   /* range of values for plot of concentration */
  199.  
  200.   min = -2;
  201.   max = 10;
  202.  
  203.   /* plot concentration */
  204.  
  205.   clear_screen();
  206.  
  207.   x_old = y_old = 0;  /* to quiet lint */
  208.  
  209.   for (i = 0; i < ncells; i++) {
  210.  
  211.     s = (b[i] - min) / (max - min);
  212.     y = (1 - s) * ysize;
  213.  
  214.     t = i / (float) (ncells - 1);
  215.     x = t * xsize;
  216.  
  217.     if (i != 0)
  218.       line (x, y, x_old, y_old, WHITE);
  219.  
  220.     x_old = x;
  221.     y_old = y;
  222.   }
  223.  
  224.   flushbuffers();
  225. }
  226.  
  227.  
  228. /******************************************************************************
  229. Pick a random number between min and max.
  230. ******************************************************************************/
  231.  
  232. float frand(min, max)
  233.   float min,max;
  234. {
  235.   return (min + drand48() * (max - min));
  236. }
  237.  
  238.